package com.dcits.electronic_account.controller;

import com.dcits.electronic_account.bean.ResponseJson;
import com.dcits.electronic_account.bean.VirtualAccount;
import com.dcits.electronic_account.bean.VirtualBank;
import com.dcits.electronic_account.constant.Constant;
import com.dcits.electronic_account.dto.BindingBankDTO;
import com.dcits.electronic_account.dto.CancelBindingDTO;
import com.dcits.electronic_account.service.RechargeService;
import com.dcits.electronic_account.service.VirtualBankService;
import com.dcits.electronic_account.vo.BindingRelationsVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author 魏渝辉
 * @Date:2022年06月25日 19:16
 * @Description:
 */

@RestController
@RequestMapping("/user")
@Slf4j
public class VirtualBankController {

    @Autowired
    private VirtualBankService virtualBankService;

    @Autowired
    private RechargeService rechargeService;

    @PostMapping("/bindingBankCard")
    public ResponseJson binding(@RequestBody BindingBankDTO bindingBankDTO){
        //先检验是否绑过此卡
        if(virtualBankService.checkBinding(bindingBankDTO)){
            //根据id查出用户信息
            VirtualAccount virtualAccount = rechargeService.searchVirByUserId(bindingBankDTO.getId());
            //将姓名和身份证信息插入到DTO中
            bindingBankDTO.setName(virtualAccount.getName());
            bindingBankDTO.setIdCard(virtualAccount.getIdNumber());
            log.info("DTO:{}",bindingBankDTO);
            Map<String, String> msgMap = virtualBankService.bindingBankCard(bindingBankDTO);
            //绑定
            if(msgMap.containsKey("success")){
                return new ResponseJson(Constant.SUCCESS_CODE,msgMap.get("success"));
            }else{
                return new ResponseJson(Constant.FAIL_CODE,msgMap.get("fail"));
            }
        }else{
            return new ResponseJson(Constant.FAIL_CODE,"此卡已被绑定");
        }
    }

    @PostMapping("/cancelBindingRelation")
    public ResponseJson cancelBindingRelation(@RequestBody CancelBindingDTO cancelBindingDTO){
        Integer integer = virtualBankService.cancelBinding(cancelBindingDTO);
        if(integer > 0){
            return new ResponseJson(Constant.SUCCESS_CODE,"银行卡解绑成功");
        }else{
            return new ResponseJson(Constant.FAIL_CODE,"银行卡解绑失败,请重试");
        }
    }


    @GetMapping("/bindingRelations/{id}")
    public ResponseJson getRelations(@PathVariable("id") Integer userId){
        List<BindingRelationsVO> virtualBanks = virtualBankService.existBank(userId);
        if(!virtualBanks.isEmpty()){
            return new ResponseJson(Constant.SUCCESS_CODE,virtualBanks);
        }else{
            return new ResponseJson(Constant.FAIL_CODE,"暂未绑定银行卡");

        }
    }

}
